/*
 * CAllMarketData.h
 *
 *  Created on: 2011-11-18
 *      Author: Owner
 */

#ifndef CALLMARKETDATA_H_
#define CALLMARKETDATA_H_

#include "Lock.h"
#include "InstrumentMarketData.h"
#include <iostream>
#include <string>
#include <map>
#include <list>

using namespace std;

class CAllMarketData {
public:
	CAllMarketData(){
	}

	virtual ~CAllMarketData(){
		Close();
	}

	MarketDataField*	AddMarketDataField(CThostFtdcDepthMarketDataField *pField){
		CInstrumentMarketData *pData = NULL;
		map<string,CInstrumentMarketData *>::iterator it = m_MarketData.find(pField->InstrumentID);
		if(it != m_MarketData.end()){
			pData = it->second;
		}else{
			pData = InitializeData4Instrument(pField->InstrumentID,pField);
		}

		return pData->AddMarketData(pField);
	}
	
	void Close(){
		map<string,CInstrumentMarketData *>::iterator it =m_MarketData.begin();
		map<string,CInstrumentMarketData *>::iterator end =m_MarketData.end();
		while(it != end){
			delete it->second;
			it++;
		}
		m_MarketData.clear();
	}
	
	CInstrumentMarketData* 	InitializeData4Instrument(const char *pszIns,CThostFtdcDepthMarketDataField *pInitial){
		CInstrumentMarketData * pNew = new CInstrumentMarketData();
		pNew->Init(pInitial);
		m_MarketData.insert(pair<string,CInstrumentMarketData *>(pszIns,pNew));
		return pNew;
	}

private:
	map<string,CInstrumentMarketData *> m_MarketData;
};

#endif /* CALLMARKETDATA_H_ */
